package com.yiren.manager.ctrl.pension;

import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.StringUtil;
import com.yiren.core.exception.MyException;
import com.yiren.core.exception.SwallowException;
import com.yiren.core.repository.utils.QueryResult;
import com.yiren.core.utils.FileUtil;
import com.yiren.core.utils.HTTPResult;
import com.yiren.core.utils.JsonMapperTool;
import com.yiren.core.utils.PropertyConfigurer;
import com.yiren.core.utils.UUIDGenerator;
import com.yiren.entity.YrLeaseSupply;
import com.yiren.manager.ctrl.GenericController;
import com.yiren.manager.form.pension.YrLeaseSupplyForm;
import com.yiren.manager.service.iface.pension.YrLeaseSupplyService;
import com.yiren.manager.utils.ObjectUtils;

@RequestMapping("leaseSupplies")
@Controller
public class YrLeaseSuppliesController extends GenericController<YrLeaseSupplyForm, YrLeaseSupply> {

	@Autowired
	private YrLeaseSupplyService selfService;

	// 保存实体
	@RequestMapping("/save")
	@ResponseBody
	public HTTPResult<YrLeaseSupplyForm> save(String data) throws SwallowException, MyException, InstantiationException,
			IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException {
		HTTPResult<YrLeaseSupplyForm> result = new HTTPResult<YrLeaseSupplyForm>();
		JsonMapperTool tool = new JsonMapperTool(data);
		YrLeaseSupplyForm form = tool.mapper("leaseSupplies", YrLeaseSupplyForm.class);
		if(null==form){
			throwExp("param.error");
		}
		if (null == form.getLeaseSuppliesId() || form.getLeaseSuppliesId().isEmpty()) {
			form.setLeaseSuppliesId(UUIDGenerator.getUUID());
			form.setCreateTime(new Date());
			form.setExistStatus(0);
		}
		form.setUpdateTime(new Date());
		if (null != form.getImgBase64() && !form.getImgBase64().isEmpty()) {
			YrLeaseSupply entity = selfService.findOne(form.getLeaseSuppliesId());
			if (entity != null) {
				FileUtil.deleteFile(String.valueOf(PropertyConfigurer.getContextProperty("yiren.img.save.url"))+entity.getIconUrl());
			}
			form.setIconUrl(FileUtil.saveBase64(form.getImgBase64(),
					String.valueOf(PropertyConfigurer.getContextProperty("yiren.img.save.url"))));
			
		}
		YrLeaseSupply entity = ObjectUtils.objectToObject(form, YrLeaseSupply.class);
		selfService.save(entity);

		return result;
	}

	/** 删除一条记录 */
	@RequestMapping("delete")
	@ResponseBody
	public HTTPResult<YrLeaseSupplyForm> delete(String id) throws MyException {
		HTTPResult<YrLeaseSupplyForm> result = new HTTPResult<YrLeaseSupplyForm>();
		if(StringUtil.isEmpty(id)){
			throwExp("param.error");
		}
		selfService.delete(id);
		return result;
	}

	/**
	 * 批量删除
	 * 
	 * @param data
	 * @return
	 * @throws SwallowException
	 * @throws MyException
	 */
	@RequestMapping("deleteBatch")
	@ResponseBody
	public HTTPResult<YrLeaseSupplyForm> deleteBatch(String data) throws SwallowException, MyException {
		HTTPResult<YrLeaseSupplyForm> result = new HTTPResult<YrLeaseSupplyForm>();
		JsonMapperTool tool = new JsonMapperTool(data);
		String[] ids = tool.mapper("ids", String[].class);
		if(null==ids){
			throwExp("param.error");
		}
		for (String id : ids) {
			YrLeaseSupply leaseSupply = selfService.findOne(id);
			if(null!=leaseSupply){
				leaseSupply.setExistStatus(1);
				selfService.save(leaseSupply);
			}
		}
		return result;
	}

	/** 获取所有记录 */
	@RequestMapping("findAll")
	@ResponseBody
	public HTTPResult<YrLeaseSupplyForm> findAll() throws MyException, InstantiationException, IllegalAccessException,
			IllegalArgumentException, InvocationTargetException {
		HTTPResult<YrLeaseSupplyForm> result = new HTTPResult<YrLeaseSupplyForm>();
		String sqlWhere = "exist_status=0";
		QueryResult<YrLeaseSupply> qr = selfService.find(YrLeaseSupply.class, sqlWhere, new Object[]{});
		List<YrLeaseSupply> entityList = qr.getResultList();
		List<YrLeaseSupplyForm> formList = new ArrayList<YrLeaseSupplyForm>();
		for (YrLeaseSupply entity : entityList) {
			YrLeaseSupplyForm form = ObjectUtils.objectToObject(entity, YrLeaseSupplyForm.class);
//			if(!StringUtil.isEmpty(form.getIconUrl())){
//				form.setIconUrl(String.valueOf(PropertyConfigurer.getContextProperty("yiren.img.save.url"))+form.getIconUrl());
//			}
			formList.add(form);
		}
		result.setData(formList);
		return result;
	}

	@Override
	public YrLeaseSupplyForm toForm(YrLeaseSupply e) throws Exception {
		YrLeaseSupplyForm form = ObjectUtils.objectToObject(e, YrLeaseSupplyForm.class);
		return form;
	}

	@Override
	public YrLeaseSupply toEntity(YrLeaseSupplyForm f) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

}
